package com.gdth.bank.activity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.gdth.bank.R;
import com.gdth.bank.adapter.ImageGridAdapter;
import com.gdth.bank.adapter.ImageGridAdapter.TextCallback;
import com.gdth.bank.model.ImageItem;
import com.gdth.bank.utils.AlbumHelper;
import com.gdth.bank.utils.AppUtil;
import com.gdth.bank.widget.image.Bimp;
import com.gdth.core.annotation.view.ViewInject;
import com.gdth.core.widget.BaseActivity;
import com.gdth.database.bean.TAppFjxxb;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.GridView;

/**
 * 图片选择器
 * @author arvin
 *
 */
public class ImageGridActivity extends BaseActivity {
	public static final String EXTRA_IMAGE_LIST = "imagelist";
	public static final String EXTRA_IMAGE_CLASS = "className";
	
	@ViewInject(id=R.id.header_label_title) TextView txt_header_title;
	@ViewInject(id=R.id.header_btn_left, click="finishClick") TextView txt_header_left;
    @ViewInject(id=R.id.header_btn_right, click="onClick") TextView txt_header_right;

	@ViewInject(id=R.id.gridview, itemClick="onItemClick") GridView gridView;
	
	List<ImageItem> dataList;
	
	ImageGridAdapter adapter;
	AlbumHelper helper;
	
	private String className;

	Handler mHandler = new Handler(new Handler.Callback() {
		@Override
		public boolean handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
				case 0:
					Toast.makeText(ImageGridActivity.this, getResources().getString(R.string.param231), Toast.LENGTH_LONG).show();
					break;
	
				default:
					break;
			}
			
			return false;
		}
	});

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_image_grid);

		initView();
	}

	/**
	 * 初始化
	 */
	private void initView() {
		txt_header_title.setText(getResources().getString(R.string.param232));
		txt_header_right.setBackground(null);
		txt_header_right.setText(getResources().getString(R.string.param233));
		
		Intent intent = this.getIntent();
		className = intent.getStringExtra(EXTRA_IMAGE_CLASS);
		
		helper = AlbumHelper.getHelper();
		helper.init(getApplicationContext());

		Serializable mSerializable = getIntent().getSerializableExtra(EXTRA_IMAGE_LIST);
		dataList = (List<ImageItem>) mSerializable;
		
		gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
		adapter = new ImageGridAdapter(ImageGridActivity.this, dataList,
				mHandler);
		gridView.setAdapter(adapter);
		adapter.setTextCallback(new TextCallback() {
			public void onListen(int count) {
				txt_header_right.setText("完成" + "(" + count + ")");
			}
		});
	}
	
	/**
	 * 完成选择
	 * @param v
	 */
	public void onClick(View v) {
		// TODO Auto-generated method stub
		ArrayList<String> list = new ArrayList<String>();
		Collection<String> c = adapter.map.values();
		for (String str : c){
			list.add(str);
		}

		if (Bimp.getInstance().isAct_bool()) {
			Bimp.getInstance().setAct_bool(false);
		}
		for (int i = 0; i < list.size(); i++) {
			if (Bimp.getInstance().getCount() < 9) {
				TAppFjxxb mTAppFjxxb = new TAppFjxxb();
				mTAppFjxxb.setAppdz(list.get(i));
				//代表从相册选择的图片
				mTAppFjxxb.setSczt(-1);
				Bimp.getInstance().getDrr().add(mTAppFjxxb);
			}
		}
		if (className != null) {
			Intent intent=new Intent();
			intent.setClassName(ImageGridActivity.this, className);
			this.startActivity(intent);
		} else {
			setResult(AppUtil.activity_pic_resultcode, null);
			finish();
		}
	}
	
	/**
	 * gridview点击事件
	 * @param parent
	 * @param view
	 * @param position
	 * @param id
	 */
	public void onItemClick(AdapterView<?> parent, View view, int position, long id){
		adapter.notifyDataSetChanged();
	}
}
